/**
 * 
 * The MIT License
 * 
 * Copyright (c) 2008 the original author or authors.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.googlecode.gchartjava;

import java.util.LinkedList;
import java.util.List;

/**
 * @author Julien Chastang (julien.c.chastang at gmail dot com)
 */
abstract class AbstractPriorityChart extends AbstractLineChart {
	
	private final List<? extends PriorityPlot> lines;

	/**
	 *
	 */
	AbstractPriorityChart(List<? extends PriorityPlot> lines) {
		super(lines);
		this.lines = lines;
	}

	/**
	 *
	 */
	@Override
	protected void prepareDataInternal() {
		super.prepareDataInternal();
		final List<Color> colors        = new LinkedList<Color>();
		final List<LineStyle> lStyles   = new LinkedList<LineStyle>();
		final List<Priority> priorities = new LinkedList<Priority>();

		// Logic to make sure things stay in step.
		boolean hasColor          = false;
		boolean hasLineStyle      = false;
		boolean hasPriorities     = false;

		// Logic to make sure things stay in step continued.
		for (PriorityPlot plot : lines) {
			hasColor          |= (plot.getColor() != null);
			hasLineStyle      |= (plot.getLineStyle() != null);
			hasPriorities     |= (plot.getPriority()  != null);
		}

		// Logic to make sure things stay in step continued.
		for (PriorityPlot line : lines) {
			if (hasColor) {
				colors.add(line.getColor() != null ? line.getColor() : Color.BLACK);
			}
			if (hasLineStyle) {
				// TODO: Create some static line styles.
				lStyles.add(line.getLineStyle() != null ? line.getLineStyle() : new LineStyle(1, 1, 0));
			}
			if (hasPriorities) {
				priorities.add(line.getPriority() != null ? line.getPriority() : Priority.NORMAL);
				
			}
		}
		if (priorities.size() > 0) {
			setPriorities(priorities,colors,lStyles);
		}
	}	
	
	/**
	 * 
	 */
	private void setPriorities(final List<Priority> priorities, final List<Color> colors, final List<LineStyle> lineStyles){
		for (int i = 0; i < priorities.size(); i++) {
			if (priorities.get(i) != null){
				final Color color = (colors.size() == 0) ? Color.BLACK : colors.get(i);
				final int size = (lineStyles.size() == 0) ? 1 : lineStyles.get(i).getLineThickness();
				//D,<color>,<data set index>,<data point>,<size>,<priority>
				addMarker("D," + color + "," + i + ",0," + size + "," + priorities.get(i));
			}
		}		
	}
}
